<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Request;

class Node extends Controller
{
    public function index()
    {
        $wwb = input('param.nname');
        if (empty($wwb)) {
            $list = DB::name('node')->paginate(3);
        } else {
            $list = DB::name('node')->where('name', 'like', $wwb.'%')->paginate(3);
        }
        $page = $list->render();
        $this->assign("page",$page);
        return view('admin@node/index',['list'=>$list]);
    }

    public function add()
    {
        return view('admin@node/add');
    }

    public function doadd()
    {
        $data = $_POST;
        $result = db('node')->insert($data);
        if (!$result) {
            $this->error('添加失败');
        }

        $this->success('新增成功','admin/Node/index');
    }

    public function delete($id)
    {
        $result = db('node')->delete($id);
        if (!$result) {
            $this->error('删除失败');
        }
        $this->success('删除成功','admin/node/index');
    }

    public function edit($id)
    {
        $list = db('node')->find($id);
        return view('admin@node/edit',['data'=> $list]);
    }

    public function update(Request $request)
    {
//         var_dump($request);
//         var_dump($_POST);
        $p = $request->param();
        // var_dump($p);
        $data = [
            'name' => $p['name'],
            'mname' => $p['mname'],
            'aname' => $p['aname'],
            'status' => $p['status']
        ];
        $result = db('node')->where('id', $p['id'])->update($data);
        if ($result) {
            $this->success('修改成功', 'admin/node/index');
        }
        $this->error('修改失败');
    }



}


